home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / windownt / uupc11yt.zip / TEST / TESTULIB.C < prev    next >
C/C++ Source or Header  |  1992-12-30  |  15KB  |  545 lines

  1. /*--------------------------------------------------------------------*/
  2. /*          Program:    ulibtest.c           28 Nov 1992              */
  3. /*          Author:     Andrew H. Derbyshire                          */
  4. /*                      Kendra Electronic Wonderworks                 */
  5. /*                      P.O. Box 132                                  */
  6. /*                      Arlington, MA 02174-0002 USA                  */
  7. /*          Internet:   help@kew.com                                  */
  8. /*          Function:   Driver program for testing UUPC/extended      */
  9. /*                      ULIB communications packages                  */
  10. /*--------------------------------------------------------------------*/
  11.  
  12. /*--------------------------------------------------------------------*/
  13. /*       Copyright (c) 1992 by Kendra Electronic Wonderworks;         */
  14. /*       all rights reserved except those explicitly granted          */
  15. /*       through the UUPC/extended license.                           */
  16. /*--------------------------------------------------------------------*/
  17.  
  18. /*
  19.  *    $Id: TESTULIB.C 1.2 1992/12/30 12:26:32 plummer Exp $
  20.  *
  21.  *    $Log: TESTULIB.C $
  22.  * Revision 1.2  1992/12/30  12:26:32  plummer
  23.  * Report errors
  24.  *
  25.  * Revision 1.1  1992/11/29  22:09:10  ahd
  26.  * Initial revision
  27.  *
  28.  */
  29.  
  30. /*--------------------------------------------------------------------*/
  31. /*                        System include files                        */
  32. /*--------------------------------------------------------------------*/
  33.  
  34. #include <stdio.h>
  35. #include <io.h>
  36. #include <string.h>
  37. #include <ctype.h>
  38. #include <stdlib.h>
  39. #include <signal.h>
  40.  
  41. /*--------------------------------------------------------------------*/
  42. /*                    UUPC/extended include files                     */
  43. /*--------------------------------------------------------------------*/
  44.  
  45. #include "lib.h"
  46. #include "hlib.h"
  47. #include "ulib.h"
  48. #include "catcher.h"
  49. #include "timestmp.h"
  50. #include "comm.h"
  51.  
  52. /*--------------------------------------------------------------------*/
  53. /*                          Global variables                          */
  54. /*--------------------------------------------------------------------*/
  55.  
  56. static char text[] =
  57. "Only the first letter of each command is examined; operands are\n"
  58. "are seperated by white space.  Defaults for commands are in\n"
  59. "parentheses.  Commands may be issued out of sequence, in which case\n"
  60. "the results are unpredictable.\n\n"
  61. "open [port [speed [direct]]]\t(COM1, 2400, and 0 (modem))\n"
  62. "close\n"
  63. "send [text]\t\t\t(this help text)\n"
  64. "Send [file]\t\t\t(console input)\n"
  65. "receive [timeout]\t\t(5 seconds for up to 512 characters)\n"
  66. "Receive [timeout] [file]\t(5 seconds, received.dat)\n"
  67. "hangup\n"
  68. "?\n"
  69. "debuglevel (4)\n"
  70. "quit\n";
  71.  
  72. static int opened = 0;
  73.  
  74. currentfile();
  75.  
  76. /*--------------------------------------------------------------------*/
  77. /*    h e l p                                                         */
  78. /*                                                                    */
  79. /*    Print help text                                                 */
  80. /*--------------------------------------------------------------------*/
  81.  
  82. static void help( void )
  83. {
  84.    puts(text);
  85. }
  86.  
  87.  
  88. int CheckErrors( void )
  89. {
  90.    int far *stats;
  91.  
  92.    stats = com_errors();
  93.  
  94.    if( stats[COM_EOVFLOW] ) return( TRUE );
  95.    if( stats[COM_EOVRUN] ) return( TRUE );
  96.    if( stats[COM_EBREAK] ) return( TRUE );
  97.    if( stats[COM_EFRAME] ) return( TRUE );
  98.    if( stats[COM_EPARITY] ) return( TRUE );
  99.    if( stats[COM_EXMIT] ) return( TRUE );
  100.    if( stats[COM_EDSR] ) return( TRUE );
  101.    if( stats[COM_ECTS] ) return( TRUE );
  102.    return( FALSE );
  103. }
  104.  
  105.  
  106. void PrintErrors( void )
  107. {
  108.    int far *stats;
  109.  
  110.    stats = com_errors();
  111.  
  112.    printmsg(3, "Buffer overflows: %-4d", stats[COM_EOVFLOW]);
  113.    printmsg(3, "Receive overruns: %-4d", stats[COM_EOVRUN]);
  114.    printmsg(3, "Break characters: %-4d", stats[COM_EBREAK]);
  115.    printmsg(3, "Framing errors:   %-4d", stats[COM_EFRAME]);
  116.    printmsg(3, "Parity errors:    %-4d", stats[COM_EPARITY]);
  117.    printmsg(3, "Transmit errors:  %-4d", stats[COM_EXMIT]);
  118.    printmsg(3, "DSR errors:       %-4d", stats[COM_EDSR]);
  119.    printmsg(3, "CTS errors:       %-4d", stats[COM_ECTS]);
  120. }
  121.  
  122.  
  123.  
  124. /*--------------------------------------------------------------------*/
  125. /*    o p e n i t                                                     */
  126. /*                                                                    */
  127. /*    Open communications port                                        */
  128. /*--------------------------------------------------------------------*/
  129.  
  130. static void openit( char *buf )
  131. {
  132.    char *token,
  133.         *port = "COM1";
  134.     BPS speed = 2400;
  135.     int direct = 0;
  136.  
  137.    token  = strtok( buf , WHITESPACE );
  138.    if ( token != NULL )
  139.    {
  140.       port = token;
  141.       token  = strtok( NULL , WHITESPACE );
  142.    }
  143.  
  144.    if ( token != NULL )
  145.    {
  146.       speed = atoi(token);
  147.       token  = strtok( NULL , WHITESPACE );
  148.    }
  149.  
  150.    if ( token != NULL )
  151.       direct = atoi(token);
  152.  
  153.    strupr( port );
  154.    printf("openline( %s, %d, %d ) -- ", port, speed, direct );
  155.    if ( CheckErrors() ) PrintErrors();
  156.   if ( openline( port, speed,  direct))
  157.       printf("failed\n");
  158.    else {
  159.       printf("succeeded\n");
  160.       opened = 1;
  161.    }
  162.    if ( CheckErrors() ) PrintErrors();
  163. } /* openit */
  164.  
  165. /*--------------------------------------------------------------------*/
  166. /*    s e n d i t                                                     */
  167. /*                                                                    */
  168. /*    Send text to communications port                                */
  169. /*--------------------------------------------------------------------*/
  170.  
  171. static void sendit( char *buf )
  172. {
  173.    char *first = buf;
  174.    int len;
  175.  
  176.    if ( ! opened )
  177.    {
  178.       puts("Port isn't open ... issue o command first");
  179.       return;
  180.    }
  181.  
  182.    while( (first != NULL ) && *first && ! isprint(*first))
  183.       first++;
  184.  
  185.    if (( first == NULL ) || ! (*first))
  186.       first = text;
  187.    else
  188.       strcat( first, "\r\n" );
  189.  
  190.    len = strlen( first );
  191.    printf( "swrite( <text>, %d) -- ", len );
  192.    len = swrite( first, len );
  193.    printf("%d characters written\n", len);
  194.    if ( CheckErrors() ) PrintErrors();
  195.  
  196. } /* sendit */
  197.  
  198. /*--------------------------------------------------------------------*/
  199. /*    r e c v e i v e i t                                             */
  200. /*                                                                    */
  201. /*    Receive data to console from serial port                        */
  202. /*--------------------------------------------------------------------*/
  203.  
  204. static void receiveit( char *buf )
  205. {
  206.  
  207.    char *token;
  208.    int timeout = 5;
  209.    int len = 512;
  210.    int actual = 0;
  211.  
  212.    if ( ! opened )
  213.    {
  214.       puts("Port isn't open ... issue o command first");
  215.       return;
  216.    }
  217.  
  218.    token  = strtok( buf , WHITESPACE );
  219.    if ( token != NULL )
  220.    {
  221.       timeout = atoi( token );
  222.       token  = strtok( NULL , WHITESPACE );
  223.    }
  224.  
  225.    if ( token != NULL )
  226.       len = atoi( token );
  227.  
  228.    token = malloc( len + 1 );
  229.  
  230.    while( (actual < len))
  231.    {
  232.       if ( actual )
  233.          len = actual;
  234.       printf("sread( <buffer>,  %d, %d ) -- ", len, timeout );
  235.       actual = sread( token, len, timeout );
  236.       printf( "%d characters available\n", actual);
  237.       if ( CheckErrors() ) PrintErrors();
  238.       timeout = 0;
  239.  
  240.       if ( terminate_processing )
  241.          return;
  242.  
  243.       if ( !actual )
  244.          break;
  245.    }
  246.  
  247.    if ( actual )
  248.    {
  249.       token[actual] = '\0';
  250.       puts(token);
  251.    } /* if */
  252.  
  253.    free( token );
  254.  
  255. } /* receiveit */
  256.  
  257. /*--------------------------------------------------------------------*/
  258. /*    s e n d f i l e                                                 */
  259. /*                                                                    */
  260. /*    Send text to communications port from file                      */
  261. /*--------------------------------------------------------------------*/
  262.  
  263. static void sendfile( char *buf )
  264. {
  265.    char *fname = strtok( buf, WHITESPACE );
  266.  
  267.    FILE *stream;
  268.  
  269.    if ( ! opened )
  270.    {
  271.       puts("Port isn't open ... issue o command first");
  272.       return;
  273.    }
  274.  
  275.    if ( fname == NULL )
  276.       fname = "CON";
  277.  
  278.    stream = fopen( fname, "rb" );
  279.  
  280.    if ( stream == NULL )
  281.    {
  282.       perror( fname );
  283.       return;
  284.    }
  285.  
  286.    printf("Reading data from %s:\n",fname );
  287.    if ( CheckErrors() ) PrintErrors();
  288.  
  289.    for ( ;; )
  290.    {
  291.       char buf[BUFSIZ];
  292.       int len;
  293.       int actual;
  294.  
  295.       len = fread( buf, sizeof *buf, sizeof buf, stream );
  296.       if ( ferror( stream ))
  297.       {
  298.          perror( fname );
  299.          return;
  300.       }
  301.  
  302.       if ( feof( stream ))
  303.       {
  304.          puts("EOF\n");
  305.          return;
  306.       }
  307.  
  308.       printf( "swrite( <text>, %d) -- ", len );
  309.       actual = swrite( buf, len );
  310.       printf("%d characters written\n", actual);
  311.      if ( CheckErrors() ) PrintErrors();
  312.  
  313.       if ( terminate_processing )
  314.         return;
  315.  
  316.       if ( actual != len)
  317.          break;
  318.    }
  319.    if ( CheckErrors() ) PrintErrors();
  320.  
  321. } /* sendfile */
  322.  
  323. /*--------------------------------------------------------------------*/
  324. /*    r e c e i v e f i l e                                           */
  325. /*                                                                    */
  326. /*    Receive data to console from serial port                        */
  327. /*--------------------------------------------------------------------*/
  328.  
  329. static void receivefile( char *buf )
  330. {
  331.  
  332.    char *token;
  333.    char *fname = "received.dat";
  334.    int timeout = 5;
  335.    FILE *stream;
  336.    int len = BUFSIZ;
  337.  
  338.    if ( ! opened )
  339.    {
  340.       puts("Port isn't open ... issue o command first");
  341.       return;
  342.    }
  343.  
  344.    token  = strtok( buf , WHITESPACE );
  345.    if ( token != NULL )
  346.    {
  347.       timeout = atoi( token );
  348.       token  = strtok( NULL , WHITESPACE );
  349.    }
  350.  
  351.    if ( token != NULL )
  352.    {
  353.       len = atoi( token );
  354.       token  = strtok( NULL , WHITESPACE );
  355.    }
  356.  
  357.    if ( token != NULL )
  358.       fname = token;
  359.  
  360.    stream  = fopen( fname, "ab" );
  361.    if ( stream == NULL )
  362.    {
  363.       perror( fname );
  364.       return;
  365.    }
  366.  
  367.    for ( ;; )
  368.    {
  369.  
  370.       char buf[512];
  371.       int actual = 0;
  372.  
  373.       while( (len > actual) && (len) )
  374.       {
  375.          if ( actual )
  376.          {
  377.             len = actual;
  378.             actual = 0;
  379.             timeout = 0;
  380.          }
  381.  
  382.          printf("sread( <buffer>,  %d, %d ) -- ", len, timeout );
  383.          actual = sread( buf, len, timeout );
  384.          printf( "%d characters available\n", actual);
  385.          if ( CheckErrors() ) PrintErrors();
  386.  
  387.          if ( terminate_processing )
  388.              return;
  389.  
  390.       } /* while */
  391.  
  392.       if ( len > 0 )
  393.       {
  394.          actual = fwrite( buf, sizeof *buf, len, stream );
  395.  
  396.          if ( actual != len )
  397.          {
  398.             perror( fname );
  399.             return;
  400.          } /* if */
  401.  
  402.       } /* if */
  403.       else
  404.          break;
  405.  
  406.    } /* for */
  407.  
  408.    fclose( stream );
  409.  
  410. } /* receivefile */
  411.  
  412. static void shutdown( void )
  413. {
  414.    if ( opened )
  415.       closeline();
  416.    if ( CheckErrors() ) PrintErrors();
  417. }
  418.  
  419. /*--------------------------------------------------------------------*/
  420. /*    m a i n                                                         */
  421. /*                                                                    */
  422. /*    main program                                                    */
  423. /*--------------------------------------------------------------------*/
  424.  
  425. void main( int argc, char ** argv )
  426. {
  427.    char buf[BUFSIZ];
  428.    int done = 0;
  429.    char *next, *command;
  430.  
  431.    banner( argv );
  432.  
  433.    if (!configure( B_MUA ))
  434.       exit(1);    /* system configuration failed */
  435.  
  436.    debuglevel = 4;
  437.  
  438. /*--------------------------------------------------------------------*/
  439. /*                        Trap control C exits                        */
  440. /*--------------------------------------------------------------------*/
  441.  
  442.     if( signal( SIGINT, ctrlchandler ) == SIG_ERR )
  443.     {
  444.         printmsg( 0, "Couldn't set SIGINT\n" );
  445.         panic();
  446.     }
  447.  
  448.    if (!configure( B_MUA ))
  449.       exit(1);    /* system configuration failed */
  450.  
  451. /*--------------------------------------------------------------------*/
  452. /*                 Insure comm port is closed at exit                 */
  453. /*--------------------------------------------------------------------*/
  454.  
  455.    atexit( shutdown );
  456.    interactive_processing = FALSE;     /* Quit immediately           */
  457.  
  458.    while ( ! done )
  459.    {
  460.       if ( terminate_processing )
  461.          break;
  462.  
  463.       if ( opened )
  464.          printf("Opened, debuglevel %d, CD reports: %s\n",
  465.                   debuglevel,
  466.                   CD() ? "TRUE" : "FALSE" );
  467.       else
  468.          printf("Closed, debuglevel %d\n", debuglevel );
  469.  
  470.       printf("Enter command (? for help): ");
  471.  
  472.       if ( terminate_processing )
  473.          break;
  474.  
  475.       if ( fgets( buf, sizeof buf, stdin ) == NULL)
  476.          break;
  477.  
  478.       fputc('\n',stdout );
  479.       command = strtok( buf, WHITESPACE );
  480.       next    = strtok( NULL, "\r\n" );
  481.  
  482.       switch( *command )
  483.       {
  484.          case 'o':
  485.             openit( next );
  486.             break;
  487.  
  488.          case 'c':
  489.             printf("closeline() --");
  490.             closeline();
  491.             opened = 0;
  492.             fputc('\n', stdout);
  493.             break;
  494.  
  495.          case 's':
  496.             sendit( next );
  497.             break;
  498.  
  499.          case 'r':
  500.             receiveit( next );
  501.             break;
  502.  
  503.          case 'S':
  504.             sendfile( next );
  505.             break;
  506.  
  507.          case 'R':
  508.             receivefile( next );
  509.             break;
  510.  
  511.          case 'h':
  512.             printf("hangup() --");
  513.             hangup();
  514.             fputc('\n', stdout);
  515.             break;
  516.  
  517.          case '?':
  518.             help();
  519.             break;
  520.  
  521.          case 'q':
  522.             done = 1;
  523.             break;
  524.  
  525.          case 'd':
  526.             next = strtok( next, WHITESPACE );
  527.             if ( next != NULL )
  528.                debuglevel = atoi( next );
  529.             else
  530.                debuglevel = 4;
  531.             break;
  532.  
  533.          default:
  534.             printf("Invalid command '%c', try ? for help\n",
  535.                      *command );
  536.             break;
  537.  
  538.       } /* switch */
  539.    if ( CheckErrors() ) PrintErrors();
  540.    } /* for */
  541.  
  542.    exit(0);
  543.  
  544. } /* main */
  545.